package com.example.testapp

import android.app.Application
import android.util.Log
import com.blankj.utilcode.util.DeviceUtils
import com.qiantoon.networkdsl.base.*
import com.qiantoon.networkdsl.http.BaseResponseBean
import com.qiantoon.networkdsl.support.GsonUtils
import com.qiantoon.networkdsl.support.PreferencesUtil
import com.qiantoon.networkdsl.utils.QianToonApiUtil
import okhttp3.RequestBody
import java.lang.Exception

/**
 * @Package com.example.testapp
 * @ClassName MoreUrlInfo
 * @Author caostgrace
 * @Date 2021/4/12
 * @Email liaobing@qiantoon.com
 * @Desc
 */
class MoreUrlInfo : IMoreUrlInfo {
    override fun convertUrl(urlMappingName: String?) = when (urlMappingName) {
        DOCTOR_HEALTHY -> urlFormat(BuildConfig.HEALTHYCIRCLE_BASE_URL, BuildConfig.HEALTHYCIRCLE_PORT)
        else -> null
    }

    override fun isSupport(urlMappingName: String?) = when (urlMappingName) {
        DOCTOR_HEALTHY -> true
        else -> false
    }

    companion object {
        const val DOCTOR_HEALTHY = "doctor_healthy"
    }
}

class NetWorkOp : INetWorkOp {
    override suspend fun getToken(): BaseResponseBean {
        val map: MutableMap<String, String> = HashMap()
        map["appid"] = QianToonApiUtil.QTDOCTOR_APP_ID
        map["appsecret"] = QianToonApiUtil.QTDOCTOR_APP_SECRET
        map["OrgCode"] = "Android" //系统平台类型，Android,IOS
        val body = RequestBody.create(QianToonApiUtil.getDefaultMediaType(), GsonUtils.toJson(map))
        return PreNetworkApi
                .getService(DoctorService::class.java)
                .getToken(body)
    }

    override fun handle105() {
        super.handle105()
    }

    override fun handle202() {
        super.handle202()
    }
}

class NetworkRequiredInfo(val app: Application) : INetworkRequiredInfo {

    override fun getApplicationContext() = app

    override fun getNetWorkOp() = NetWorkOp()

    override fun getAppVersionCode() = BuildConfig.VERSION_CODE.toString()

    override fun getPort() = BuildConfig.PORT

    override fun getBaseUrl() = BuildConfig.BASE_URL

    override fun isDebug() = BuildConfig.DEBUG

    override fun getInstance() = DoctorNetWorkApi()

    override fun getAppVersionName() = BuildConfig.VERSION_NAME

    override fun getMoreUrlInfo() = MoreUrlInfo()

}

class DoctorNetWorkApi : PreNetworkApi() {

    companion object {
        const val URL = "http://%s:%s/QiantoonService/"
    }

    override fun getCommonParameter(): MutableMap<String, String> {
        val paramsMap = mutableMapOf<String, String>()
        paramsMap.put("SystemType", "Android") //系统平台类型，Android,IOS
        paramsMap.put("DeviceId", DeviceUtils.getUniqueDeviceId()) //唯一设备号
        try {
            var userInfo = HashMap<String, String>()
            userInfo = PreferencesUtil.getInstance().getUserInfo(userInfo.javaClass)
            if (userInfo != null) {
                paramsMap.put("UserToken", userInfo["Token"]!!)
                paramsMap.put("OperID", userInfo["OperID"]!!)
                //医生端
                paramsMap.put("DocID", userInfo["DocID"]!!)
                paramsMap.put("OrgCode", userInfo["OrgCode"]!!)
                paramsMap.put("DepartCode", userInfo["DepartCode"]!!)
            }
        } catch (e: Exception) {
        }
        return paramsMap
    }

    override fun getFormal(): String {
        return String.format(URL, iNetworkRequiredInfo.baseUrl, iNetworkRequiredInfo.port)
    }

}


